package com.example.administrator.college02.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.administrator.college02.helper.AnalyzeJson;
import com.example.administrator.college02.BaseFragment;
import com.example.administrator.college02.R;
import com.example.administrator.college02.pager.CycleViewPager;
import com.example.administrator.college02.widget.KyTile;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/11.
 */
public class LifeFragment extends BaseFragment {

    private View headView;
    @BindView(R.id.btn_taoyu) KyTile btnTaoyu;
    @BindView(R.id.btn_jianzhi) KyTile btnJianzhi;
    @BindView(R.id.btn_shudong) KyTile btnShudong;
    @BindView(R.id.btn_yiqiwan) KyTile btnYiqiwan;
    @BindView(R.id.btn_huatiquan) KyTile btnHuatiquan;
    @BindView(R.id.life_listview) ListView mListView;
    private ImageView bannerImageView = null;
    private List<ImageView> mImageViews = new ArrayList<>();
    private Context mContext;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 0x123:
                    AnalyzeJson.setBanners(mContext,mImageViews,banners,bannerImageView,mCycleViewPager);
                    break;
            }
        }
    } ;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_life_layout,container,false);
        unbinder = ButterKnife.bind(this,view);
        mContext = getContext();
        banners = new ArrayList<>();
        headView = inflater.inflate(R.layout.head_lv_scroll,container ,false);
        ListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        headView.setLayoutParams(params);
        mCycleViewPager = (CycleViewPager) getChildFragmentManager().findFragmentById(R.id.life_fragment_banner);
        AnalyzeJson.initBanner(mContext, mImageViews,banners,mCycleViewPager);
        AnalyzeJson.getDataFromSchoolServer(banners,handler,mCycleViewPager);
        mListView.addHeaderView(headView);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        btnTaoyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBaseActivity.showFragment(TaoyuListFragment.class,"123",null,true);
            }
        });
        btnShudong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBaseActivity.showFragment(FragmentShudong.class,"123",null,true);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
